package com.scott.masterselector.zkclient;

import org.I0Itec.zkclient.ZkClient;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author Scott 2017/11/9 14:00
 */
public class MasterSelectorTestDemo {
    /**
     * zookeeper 连接字符串
     */
    private final static String CONNECT_STRING = "192.168.1.113:2181,192.168.11.104:2181";

    public static void main(String[] args) {
        List<MasterSelector> selectorList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            ZkClient client = new ZkClient(CONNECT_STRING, 500, 1000);
            UserCenterService service = new UserCenterService();
            service.setMcId(i);
            service.setMcName(String.format("机器%d", i));
            MasterSelector selector = new MasterSelector(client, service);

            selector.start();
            selectorList.add(selector);
        }

        ScheduledExecutorService service = Executors.newScheduledThreadPool(1);


        service.scheduleAtFixedRate(() -> {
           //找到master节点，把他停掉
            System.out.println("===============定时任务启动=============");
            for(MasterSelector selector:selectorList){
                if(selector.getMaster()==selector.getService()){
                    selector.stopService();
                }
            }
        }, 2,5, TimeUnit.SECONDS);



    }
}
